﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.Data.Filtering;
using System.Reflection;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.ViewInfo;
using System.Drawing;
using DevExpress.XtraEditors.Controls;


namespace QuanLyThuNgan
{
    public class ClassHelp
    {

        public static void m_LookUpEdit(DataTable _dt, LookUpEdit col, string strTen, string strID)
        {
            if (_dt == null) return;
            col.Properties.DisplayMember = strTen;
            col.Properties.ValueMember = strID;
            col.Properties.DataSource = _dt;
            col.Properties.TextEditStyle = TextEditStyles.Standard;
        }
        public static void m_SearchLookUpEdit(DataTable _dt, SearchLookUpEdit col, string strTen, string strID)
        {
            if (_dt == null) return;
            col.Properties.DisplayMember = strTen;
            col.Properties.ValueMember = strID;
            col.Properties.DataSource = _dt;
            col.Properties.TextEditStyle = TextEditStyles.Standard;
            
        }
        public static void m_LoadLookupEditIngridView(DataTable _dt, DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit _lo, string strTen, string strID)
        {
            //foreach (DataRow dr in _dt.Rows)
            //{
            //    dr[strTen] = VNInUnicode.Vni2Unicode(dr[strTen].ToString());
            //}
            _lo.Properties.DataSource = _dt;
            _lo.Properties.DisplayMember = strTen;
            _lo.Properties.ValueMember = strID;
            _lo.Properties.TextEditStyle = TextEditStyles.Standard;
            //_lo.Properties.Columns[strID].Visible = false;

        }
        public static void m_RepositoryItenSearchLookUpEdit(DataTable _dt, DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit col, string strTen, string strID)
        {
            if (_dt == null) return;
            col.View.Columns.AddField(strTen).Visible = true;
            col.View.Columns.AddField(strID).Visible = true;
            col.View.Columns[strID].Width = 8;
            col.DisplayMember = strTen;
            col.ValueMember = strID;
            col.DataSource = _dt;
            col.TextEditStyle = TextEditStyles.Standard;
        }
        public static void m_RepositoryItenSearchLookUpEdit_View(DataView _dt, DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit col, string strTen, string strID)
        {
            if (_dt == null) return;
            col.View.Columns.AddField(strTen).Visible = true;
            col.View.Columns.AddField(strID).Visible = true;
            col.View.Columns[strID].Width = 8;
            col.DisplayMember = strTen;
            col.ValueMember = strID;
            col.DataSource = _dt;
            col.TextEditStyle = TextEditStyles.Standard;
        }
        //load checkedcomboboxEdit----------------------------------------------------
        public static void m_CheckComboboxEdit(DataTable _dt, CheckedComboBoxEdit _chk, string _strTen, string _strID)
        {
            if (_dt == null)
                return;
            _chk.Properties.DataSource = _dt;
            _chk.Properties.DisplayMember = _strTen;
            _chk.Properties.ValueMember = _strID;
        }

       public static void m_gridLookUpEdit(DataTable _dt, GridLookUpEdit col, string strTen, string strID)
        {
            if (_dt == null) return;
            foreach (DataRow dr in _dt.Rows)
            {
                dr[strTen] = Uit.it_Font.VNIToUnicode(dr[strTen].ToString());
            }
            col.Properties.DisplayMember = strTen;
            col.Properties.ValueMember = strID;
            col.Properties.DataSource = _dt;
            col.Properties.TextEditStyle = TextEditStyles.Standard;
        }
       public static void m_LoadLookupEditIngridView(DataTable _dt, DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit _lo, string strTen, string strID)
       {
           if (_dt == null) return;
           foreach (DataRow dr in _dt.Rows)
           {
               dr[strTen] = Uit.it_Font.VNIToUnicode(dr[strTen].ToString());
           }
           _lo.DataSource = _dt;
           _lo.DisplayMember = strTen;
           _lo.ValueMember = strID;
           _lo.TextEditStyle = TextEditStyles.Standard;
       }
       public static void m_gridLookUpEdit_seach(GridLookUpEdit _gle,string _column,string _values )
       {
           ColumnView view = _gle.Properties.View;
           view.ActiveFilter.Add(view.Columns[_column],
           new ColumnFilterInfo("[" + _column + "] Like '%" + _values + "%'", ""));
       }
       public static void m_gridView_seach(GridView _gle, string _column, string _values)
       {
           ColumnView view = _gle;
           view.ActiveFilter.Add(view.Columns[_column],
           new ColumnFilterInfo("[" + _column + "] Like '%" + _values + "%'",""));
       }

        // 
       public static CustomDevExpressGridLookUpEdit CreateCustomGridLookupEdit(GridControl gcstring, string CaptionTenDV, string displayMember, string CaptionMaDichVu, string valueMember)
       {
           CustomDevExpressGridLookUpEdit gridLookUpEdit = new CustomDevExpressGridLookUpEdit();
           gridLookUpEdit.AutoHeight = false;
           gridLookUpEdit.DisplayMember = displayMember;
           gridLookUpEdit.Name = Guid.NewGuid().ToString();
           gridLookUpEdit.ValueMember = valueMember;
           gridLookUpEdit.NullText = "";
           gridLookUpEdit.ImmediatePopup = true;   // show as soon as user starts typing
           gridLookUpEdit.TextEditStyle = TextEditStyles.Standard;
           gridLookUpEdit.PopupFormSize = new Size(350, 350);
           gridLookUpEdit.PopupFilterMode = PopupFilterMode.Contains;
           gridLookUpEdit.View = CreateViewForStockNumberAndDescriptionGridLookupEdit(CaptionTenDV, displayMember, CaptionMaDichVu, valueMember);
           gridLookUpEdit.AutoComplete = false;    // side effect: prevents automatically selecting first in list
           gridLookUpEdit.CloseUp += new CloseUpEventHandler(gridLookUpEdit_CloseUp);
           gcstring.RepositoryItems.Add(gridLookUpEdit);
           return gridLookUpEdit;
       }
       public static void gridLookUpEdit_CloseUp(object sender, CloseUpEventArgs e)
       {
           GridView view = (sender as DerivedLookUpEdit).Properties.View;
           e.AcceptValue = true;
           if (view.FocusedRowHandle == GridControl.InvalidRowHandle)
               e.Value = view.GetRowCellValue(0, (sender as DerivedLookUpEdit).Properties.ValueMember);
       }
       public static GridView CreateViewForStockNumberAndDescriptionGridLookupEdit(string CaptionTenDV, string tencot, string CaptionMaDichVu, string macot)
       {
           GridColumn cityColumnForGridLookupEdit1 = new GridColumn();
           cityColumnForGridLookupEdit1.Caption = CaptionTenDV;
           cityColumnForGridLookupEdit1.FieldName = tencot;
           cityColumnForGridLookupEdit1.MaxWidth = 250;
           cityColumnForGridLookupEdit1.MinWidth = 250;
           cityColumnForGridLookupEdit1.Name = Guid.NewGuid().ToString();
           cityColumnForGridLookupEdit1.OptionsColumn.AllowEdit = false;
           cityColumnForGridLookupEdit1.OptionsColumn.AllowSize = false;
           cityColumnForGridLookupEdit1.OptionsColumn.FixedWidth = true;
           cityColumnForGridLookupEdit1.OptionsFilter.AutoFilterCondition = AutoFilterCondition.Like;
           cityColumnForGridLookupEdit1.Visible = true;
           cityColumnForGridLookupEdit1.VisibleIndex = 0;
           cityColumnForGridLookupEdit1.Width = 250;
           GridColumn cityColumnForGridLookupEdit2 = new GridColumn();
           cityColumnForGridLookupEdit2.Caption = CaptionMaDichVu;
           cityColumnForGridLookupEdit2.FieldName = macot;
           cityColumnForGridLookupEdit2.MaxWidth = 100;
           cityColumnForGridLookupEdit2.MinWidth = 100;
           cityColumnForGridLookupEdit2.Name = Guid.NewGuid().ToString();
           cityColumnForGridLookupEdit2.OptionsColumn.AllowEdit = false;
           cityColumnForGridLookupEdit2.OptionsColumn.AllowSize = false;
           cityColumnForGridLookupEdit2.OptionsColumn.FixedWidth = true;
           cityColumnForGridLookupEdit2.OptionsFilter.AutoFilterCondition = AutoFilterCondition.Like;
           cityColumnForGridLookupEdit2.Visible = true;
           cityColumnForGridLookupEdit2.VisibleIndex = 1;
           cityColumnForGridLookupEdit2.Width = 100;

           GridView viewForGridLookupEdit = new GridView();
           viewForGridLookupEdit.FocusRectStyle = DrawFocusRectStyle.RowFocus;
           viewForGridLookupEdit.Name = "viewForGridLookupEdit";
           viewForGridLookupEdit.OptionsSelection.EnableAppearanceFocusedCell = false;
           viewForGridLookupEdit.OptionsView.ShowGroupPanel = false;
           //
           viewForGridLookupEdit.Columns.Add(cityColumnForGridLookupEdit1);
           viewForGridLookupEdit.Columns.Add(cityColumnForGridLookupEdit2);
           viewForGridLookupEdit.SortInfo.Add(new GridColumnSortInfo(cityColumnForGridLookupEdit1, ColumnSortOrder.Ascending));
           viewForGridLookupEdit.SortInfo.Add(new GridColumnSortInfo(cityColumnForGridLookupEdit2, ColumnSortOrder.Ascending));

           return viewForGridLookupEdit;
       }
     
    }
}
